package com.qjl.shopping.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjl.shopping.bean.Game;
import com.qjl.shopping.bean.Item;
import com.qjl.shopping.common.ResponseEntity;
import com.qjl.shopping.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author liguanghui
 * @Date 2023/5/3
 */
@RestController
public class ItemController {
    @Autowired
    private ItemService itemService;
    @GetMapping("/find/item/{id}")
    public ResponseEntity findById(@PathVariable int id){
        Item byId = itemService.getById(id);
        if(byId==null){
            return ResponseEntity.error("查询失败，确认id是否有效");
        }
        return ResponseEntity.success("根据id查询成功",byId);
    }

    //查询全部
    @GetMapping("/find/all/items")
    public ResponseEntity findAll(@RequestParam(defaultValue = "0") int pageNum,
                                  @RequestParam(defaultValue = "9") int pageSize,
                                  @RequestParam(defaultValue = "0") int gameId,
                                  @RequestParam(defaultValue = "0") int type,
                                  @RequestParam(defaultValue = "0") int rule,
                                  @RequestParam(defaultValue = "0") int status,
                                  @RequestParam(defaultValue = "") String name){
        Page<Item> page = itemService.selectPage(pageNum, pageSize,gameId,type,rule,status,name);
        return ResponseEntity.success("查询全部item成功",page);
    }

    @GetMapping("/delete/item")
    public ResponseEntity deleteType(int id){
        if(itemService.removeById(id)){
            return new ResponseEntity(200,"删除成功");
        }else {
            return new ResponseEntity(404,"删除失败");
        }
    }
    @PostMapping("/add/item")
    public ResponseEntity addType(@RequestBody Item item){
        if(itemService.save(item)){
            return new ResponseEntity(200,"增加成功");
        }else {
            return new ResponseEntity(404,"增加失败");
        }
    }
    @PostMapping("/update/item")
    public ResponseEntity update(@RequestBody Item item){
        if(itemService.updateById(item)){
            return new ResponseEntity(200,"修改成功");
        }else {
            return new ResponseEntity(404,"修改失败");
        }
    }

}
